package net.cn_su.sharpeyes.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import net.cn_su.sharpeyes.R;
import net.cn_su.sharpeyes.pref.ServerInfo_;
import net.cn_su.sharpeyes.utils.UIUtil;
import net.cn_su.sharpeyes.utils.http.HttpClient;
import net.cn_su.sharpeyes.utils.http.HttpUrl;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Fullscreen;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.sharedpreferences.Pref;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;


/**
 * Created by yzy on 2016/12/14.
 */
@EActivity(R.layout.activity_add_baoxiu)
@Fullscreen
public class BaoXiuActivity extends AppCompatActivity {
    @ViewById(R.id.tv_camera)
    TextView mTvCamera;
    @ViewById(R.id.et_content)
    EditText mEtContent;

    @ViewById(R.id.progressBar)
    ProgressBar mProgressBar;
    List<String> mTips;

    private int camera_id;
    @Pref
    ServerInfo_ mServerInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }


    @AfterViews
    public void initData() {


        mTvCamera.setText(getIntent().getStringExtra("camera"));
        camera_id = getIntent().getIntExtra("camera_id", 0);


        mTips = new ArrayList<>();
        mTips.add("监控画面模糊");
        mTips.add("图像颜色失真");
        mTips.add("黑屏、蓝屏、不显示");
        mTips.add("夜视功能损坏");
        mTips.add("视频信号时有时无");


    }


    @Click(R.id.btn_back)
    protected void btnBack(){
        finish();
    }
    @Click(R.id.tv_type_01)
    protected void btnType01(){
        setType(0);
    }
    @Click(R.id.tv_type_02)
    protected void btnType02(){
        setType(1);
    }
    @Click(R.id.tv_type_03)
    protected void btnType03(){
        setType(2);
    }
    @Click(R.id.tv_type_04)
    protected void btnType04(){
        setType(3);
    }
    @Click(R.id.tv_type_05)
    protected void btnType05(){
        setType(4);
    }
    @Click(R.id.tv_confirm)
    protected void btnConfirm(){
        if (mEtContent.getText().toString().equals("")) {
            UIUtil.showToast(this,"请填写故障问题");
            return;
        }


       if (mProgressBar!=null){
           mProgressBar.setVisibility(View.VISIBLE);
       }
        RequestParams params = new RequestParams();
        params.add("login_id", mServerInfo.userId().get().toString());
        params.add("camera_id", camera_id + "");
        params.add("source", "0");
        params.add("content", mEtContent.getText().toString().trim());
        HttpClient.post(this, HttpUrl.REPAIRS_CAMERA, params, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
         //       Logger.v(statusCode + responseString);
                if (mProgressBar!=null){
                    mProgressBar.setVisibility(View.GONE);
                }
                UIUtil.showToast(BaoXiuActivity.this,"报修摄像头失败");
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {

                if (mProgressBar!=null){
                    mProgressBar.setVisibility(View.GONE);
                }
                UIUtil.showToast(BaoXiuActivity.this, "摄像头报修成功");
                finish();

            }
        });
    }

    private void setType(int i) {

        String mContent = mEtContent.getText().toString();

        if (mContent.equals("")) {
            mContent = mTips.get(i);
        } else {
            mContent += ";" + mTips.get(i);
        }
        mEtContent.setText(mContent);
    }


}
